<template>
  <bg-vue>
    <template #imgFixed>
      <img src="@/assets/imgV2/exchange-bg-bottom.jpg" />
    </template>
    <template #img>
      <img src="@/assets/imgV2/withdraw-bg.png" />
    </template>
    <div class="withdraw-page">
      <div class="tit">
        {{ t('withdraw.TXLX') }}
      </div>
      <div class="tabs">
        <button
          @click="tabCoin('PLAY')"
          :class="{ active: coin === 'PLAY' }"
          class="l"
          >PLAY</button
        >
        <button
          @click="tabCoin('PT')"
          :class="{ active: coin === 'PT' }"
          class="r"
          >PT</button
        >
      </div>

      <div class="address">
        <div class="name">{{ t('withdraw.EA') }}</div>
        <div class="value">{{ addr }}</div>
      </div>

      <div class="info">
        <div class="name">{{ t('withdraw.TS') }}</div>
        <div class="value"
          >{{ coin }} {{ t('walletGm.KY') }}: {{ coin === 'PT' ? gm : at }}</div
        >
      </div>
      <div class="ipt">
        <van-field
          v-model="valueAt"
          type="number"
          :border="false"
          :placeholder="t('withdraw.SRSL')"
        />
      </div>
      <div class="btns">
        <van-button @click="onWithdraw">{{ t('walletGm.extract') }}</van-button>
        <router-link :to="'/drawRecord?coin=' + coin">{{
          t('withdraw.TQJL')
        }}</router-link>
      </div>
    </div>
  </bg-vue>
</template>
<script>
import { state } from '../dapp'
import http from '../http'
import BgVue from '@/components/BgVue.vue'
import removeZeros from '@/utils/remove-zeros'

export default {
  data() {
    var addr = state.address
    return {
      valueAt: '',
      at: '0',
      gm: '0',
      sum: '0',
      coin: 'PLAY',
      addr: addr.substr(0, 30) + '...' + addr.substr(38),
      // addr: addr,
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    tabCoin(coin) {
      this.coin = coin
      this.list = []
      this.init()
    },
    async init() {
      const res = await http.get('/api/user/asset')
      if (res.code === 200) {
        this.at = removeZeros(res.data.at)
        this.gm = removeZeros(res.data.gm)
      }
    },
    async onWithdraw() {
      const coinIds = {
        PT: 1,
        PLAY: 2,
      }
      const coinId = coinIds[this.coin]
      const value = this.valueAt * 1
      const balance = this.coin === 'PT' ? this.gm : this.at
      console.log(
        'withdraw',
        this.coin,
        coinId,
        value,
        balance,
        value > 0 && value <= balance,
      )
      if (value > 0 && value <= balance) {
        const res = await http.post(
          `/api/user/withdraw?amount=${value}&coinId=${coinId}`,
        )
        if (res.code === 200) {
          this.valueAt = '0'
          Toast(this.t('home.TJCG'))
          this.init()
        }
      }
    },
  },
  components: { BgVue },
}
</script>
<style lang="less" scoped>
.withdraw-page {
  .tit {
    height: 115px;
    padding: 90px 42px 0 52px;
    color: #ffc53d;
    font-size: 24px;

    text-shadow: 1px 2px 0px #953d00;
    font-weight: bold;
  }
  .tabs {
    display: flex;
    justify-content: space-between;
    padding: 43px 53px 0;

    button {
      width: 123px;
      height: 50px;

      border: none;
      color: #fff;
      font-size: 19px;
      border-radius: 12px;
    }
    button.active {
      background-image: linear-gradient(0deg, #fe7d27 0%, #ffdc43 100%);

      text-shadow: #d8540d 1px 0 2px, #d8540d 0 1px 2px, #d8540d -1px 0 2px,
        #d8540d 0 -1px 2px;
    }
    button + button {
      margin-right: -7px;
    }
  }

  .address {
    padding: 30px 54px 0 68px;
    color: #ffffff;
    .name {
      height: 20px;
      font-size: 15px;
      letter-spacing: 2px;
    }

    .value {
      height: 20px;
      font-size: 12px;
      padding-top: 4px;
      white-space: nowrap;
    }
  }

  .info {
    display: flex;
    justify-content: space-between;
    height: 40px;

    padding: 20px 48px 0 56px;
    color: #ffffff;
    font-size: 15px;
  }
  .ipt {
    padding: 18px 46px 18px 60px;

    .van-cell {
      padding: 0;
      background: transparent;
    }

    :deep(input) {
      height: 24px;
      padding: 0 8px;
      color: #117fd4;
      font-size: 16px;

      letter-spacing: 1px;

      &::placeholder {
        color: #117fd4;
      }
    }
    // color: #117fd4;
    // font-size: 16px;
    // font-weight: bold;
    // button {
    //   width: 280px;
    //   height: 42px;
    //   text-align: left;
    //   padding-left: 10px;
    // }
  }
  .btns {
    padding: 12px 120px 0 130px;
    .van-button,
    a {
      display: block;
      width: 100%;
      height: 42px;

      color: #ffffff;
      font-size: 17px;
      background-color: transparent !important;
      border: none;
      // background-image: linear-gradient(0deg, #fe7d27 0%, #ffdc43 100%),
      //   linear-gradient(#fbba1e, #fbba1e);

      // text-shadow: 0px 1px 1px #953d00;
      text-shadow: #146092 1px 0 2px, #146092 0 1px 2px, #146092 -1px 0 2px,
        #146092 0 -1px 2px;
      // font-weight: bold;
    }
    .van-button + a {
      display: flex;
      align-items: center;
      justify-content: center;
      text-decoration: underline;

      margin-left: -20px;
      width: 160px;
    }
  }
}
</style>
